package com.example.drive.material.service.impl;

import com.example.drive.coach.pojo.entity.Check;
import com.example.drive.coach.pojo.entity.Register;
import com.example.drive.common.ServiceCode;
import com.example.drive.common.ServiceException;
import com.example.drive.material.mapper.MaterialMapper;
import com.example.drive.material.pojo.Material;
import com.example.drive.material.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class MaterialServiceImpl implements MaterialService {

    @Autowired
    private MaterialMapper materialMapper;

    /**
     * 获取材料照片
     * @return
     */
    @Override
    public List<Material> getMaterial(){
        List<Material> materialList = materialMapper.getList();
        if(materialList==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在材料！");
        }
        return materialList;
    }

    /**
     * 删除打回材料
     * @param id
     */
    @Override
    public void deleteMaterial(Integer id){
       Material material= materialMapper.getById(id);
       if(material==null){
           throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在改材料!");
       }
       int result = materialMapper.deleteMaterial(id);
       if(result<=0){
           throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"打回失败！");
       }
    }

    /**
     * 获取体检表
     * @return
     */
    @Override
    public List<Check> getCheck(){
        List<Check> checkList = materialMapper.getList1();
        if(checkList==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在材料！");
        }
        return checkList;
    }

    /**
     * 删除打回体检表
     * @param id
     */
    @Override
    public void deleteCheck(Integer id){
        Check check = materialMapper.getById1(id);
        if(check==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该体检表!");
        }
        int result = materialMapper.deleteCheck(id);
        if(result<=0){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"打回失败！");
        }
    }

    /**
     * 获取注册表
     * @return
     */
    @Override
    public List<Register> getRegister(){
        List<Register> registerList = materialMapper.getList2();
        if(registerList==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在注册表！");
        }
        return registerList;
    }

    /**
     * 删除打回注册表
     * @param id
     */
    @Override
    public void deleteRegister(Integer id){
        Register register = materialMapper.getById2(id);
        if(register==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该注册表!");
        }
        int result = materialMapper.deleteRegister(id);
        if(result<=0){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"打回失败！");
        }
    }
}
